home *** CD-ROM | disk | FTP | other *** search
/ Complete Linux / Complete Linux.iso / docs / apps / database / postgres / postgre4.z / postgre4 / src / rules / prs2 / prs2impexp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-27  |  5.6 KB  |  203 lines

  1. /*====================================================================
  2.  * FILE: prs2impexp.c
  3.  *
  4.  * $Header: /private/postgres/src/rules/prs2/RCS/prs2impexp.c,v 1.2 1991/06/18 15:42:06 sp Exp $
  5.  *
  6.  * DESCRIPTION:
  7.  * routines that have to do with the import/export locking scheme.
  8.  *====================================================================
  9.  */
  10. #include "tmp/c.h"
  11. #include "utils/log.h"
  12. #include "utils/palloc.h"
  13. #include "rules/prs2.h"
  14. #include "rules/prs2stub.h"
  15. #include "parser/parse.h"    /* for APPEND, DELETE */
  16.  
  17. /*----------------------------------------------------------------------
  18.  * prs2FindNewExportLocksFromLocks
  19.  *
  20.  *----------------------------------------------------------------------
  21.  */
  22. RuleLock
  23. prs2FindNewExportLocksFromLocks(locks)
  24. RuleLock locks;
  25. {
  26.     RuleLock copylocks, expLocks, temp, thisLock;
  27.     int nlocks;
  28.     int i,j;
  29.     Prs2OneLock oneLock, oneLock2;
  30.     int npartial, partialindx;
  31.     bool *table;
  32.  
  33.     /*
  34.      * make a copy of the locks because we are going to
  35.      * update them in place...
  36.      */
  37.     copylocks = prs2CopyLocks(locks);
  38.     nlocks = prs2GetNumberOfLocks(copylocks);
  39.     expLocks = prs2MakeLocks();
  40.  
  41.     for (i=0; i<nlocks; i++) {
  42.     oneLock = prs2GetOneLockFromLocks(copylocks, i);
  43.     if (prs2OneLockGetLockType(oneLock) != LockTypeImport)
  44.         continue;
  45.     npartial = prs2OneLockGetNPartial(oneLock);
  46.     Assert(npartial != 0);
  47.     partialindx = prs2OneLockGetPartialIndx(oneLock);
  48.     table = (bool *) palloc(sizeof(bool) * npartial);
  49.     for (j=0; j<npartial; j++)
  50.         table[j] = false;
  51.     table[partialindx] = true;
  52.     /*
  53.      * now find all other import locks with the same ruleid and
  54.      * planid. Update `table' accordingly...
  55.      * NOTE: modify its 'locktype' to invalid, so that we don't
  56.      * check it again later... (hack, hack...)
  57.      */
  58.     for (j=i+1; j<nlocks; j++) {
  59.         oneLock2 = prs2GetOneLockFromLocks(copylocks, j);
  60.         if (prs2OneLockGetLockType(oneLock2) != LockTypeImport)
  61.         continue;
  62.         if (prs2OneLockGetRuleId(oneLock2) != 
  63.         prs2OneLockGetRuleId(oneLock))
  64.         continue;
  65.         if (prs2OneLockGetPlanNumber(oneLock2) != 
  66.         prs2OneLockGetPlanNumber(oneLock))
  67.         continue;
  68.         table[prs2OneLockGetPartialIndx(oneLock2)] = true;
  69.         prs2OneLockSetLockType(oneLock2, LockTypeInvalid);
  70.     }
  71.     /*
  72.      * are all the "partial" import locks of the rule there?
  73.      */
  74.     for (j=0; j<npartial; j++) {
  75.         if (!table[j])
  76.         break;
  77.     }
  78.     if (j==npartial) {
  79.         /*
  80.          * yes, there are! find the export lock and add it
  81.          * to the 'expLocks'.
  82.          * NOTE: we only add the lokcs that there were
  83.          * not already there!
  84.          *
  85.          * NOTE: prs2LockUnion & prs2LockDiffernece
  86.          * create copies, so get rid of the extra locks...
  87.          */
  88.         thisLock = prs2GetExportLockFromCatalog(
  89.                 prs2OneLockGetRuleId(oneLock),
  90.                 prs2OneLockGetPlanNumber(oneLock));
  91.         temp = prs2LockDifference(thisLock, locks);
  92.         prs2FreeLocks(thisLock);
  93.         thisLock = temp;
  94.         temp = prs2LockUnion(expLocks, thisLock);
  95.         prs2FreeLocks(thisLock);
  96.         prs2FreeLocks(expLocks);
  97.         expLocks = temp;
  98.     }
  99.     /*
  100.      * don't forget to free the table!
  101.      */
  102.     pfree(table);
  103.     } /* for i */
  104.     
  105.     /*
  106.      * cleanup & return result locks...
  107.      */
  108.     prs2FreeLocks(copylocks);
  109.     return(expLocks);
  110. }
  111.  
  112. /*--------------------------------------------------------------------
  113.  * prs2GetExportLockFromCatalog
  114.  *
  115.  * look into the system catalogs for all required info and
  116.  * construct the export lock for the given `ruleid' and `planid'.
  117.  *--------------------------------------------------------------------
  118.  */
  119. RuleLock
  120. prs2GetExportLockFromCatalog(ruleId, planNo)
  121. ObjectId ruleId;
  122. Prs2PlanNumber planNo;
  123. {
  124.  
  125.     LispValue type;
  126.     LispValue plan;
  127.     LispValue lockInfo;
  128.     RuleLock lock;
  129.  
  130.     plan = prs2GetRulePlanFromCatalog(ruleId, planNo, (ParamListInfo *) NULL);
  131.  
  132.     /*
  133.      * sanity check...
  134.      */
  135.     type = prs2GetTypeOfRulePlan(plan);
  136.     if (strcmp(CString(type), Prs2RulePlanType_EXPORT)) {
  137.     elog(WARN, "prs2GetExportLockFromCatalog: plan is not EXPORT!");
  138.     }
  139.  
  140.     /*
  141.      * extract the lock stored in the plan
  142.      */
  143.     lockInfo = prs2GetLockInfoFromExportPlan(plan);
  144.     lock = StringToRuleLock(CString(lockInfo));
  145.  
  146.     return(lock);
  147. }
  148.  
  149. /*--------------------------------------------------------------------
  150.  * prs2ActivateExportLockRulePlan
  151.  *--------------------------------------------------------------------
  152.  */
  153. void
  154. prs2ActivateExportLockRulePlan(oneLock, value, typeid, operation)
  155. Prs2OneLock oneLock;
  156. Datum value;
  157. ObjectId typeid;
  158. int operation;
  159. {
  160.     LispValue plan;
  161.     LispValue type;
  162.     ObjectId ruleId;
  163.     ParamListInfo params;
  164.     Prs2PlanNumber planNo;
  165.     Boolean isnull;
  166.     int i;
  167.  
  168.     ruleId = prs2OneLockGetRuleId(oneLock);
  169.     planNo = prs2OneLockGetPlanNumber(oneLock);
  170.  
  171.     plan = prs2GetRulePlanFromCatalog(ruleId, planNo, ¶ms);
  172.  
  173.     /*
  174.      * sanity check...
  175.      */
  176.     type = prs2GetTypeOfRulePlan(plan);
  177.     if (strcmp(CString(type), Prs2RulePlanType_EXPORT)) {
  178.     elog(WARN, "prs2GetExportLockFromCatalog: plan is not EXPORT!");
  179.     }
  180.  
  181.     /*
  182.      * `params' must contain only one parameter.
  183.      * Make it have the given `value' and `type'.
  184.      */
  185.     if (params[0].kind != PARAM_OLD)
  186.     elog(WARN, "prs2ActivateExportLockRulePlan: illegal param");
  187.     if (params[1].kind != PARAM_INVALID)
  188.     elog(WARN, "prs2ActivateExportLockRulePlan: more than 1 param");
  189.     params[0].isnull = false;
  190.     params[0].value = value;
  191.     params[0].type = typeid;
  192.     params[0].length = (Size) get_typlen(type);
  193.  
  194.     /*
  195.      * OK, now run the plan.
  196.      * XXX: should we use a non null value for Prs2EStateInfo ??
  197.      */
  198.     prs2RunActionPlans(prs2GetActionPlanFromExportPlan(plan),
  199.             params, (Prs2EStateInfo) NULL);
  200.  
  201. }
  202.  
  203.